import codecs
import datetime
import os
import inline as inline
import matplotlib
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import missingno as msno
from django.http import FileResponse
from django.shortcuts import render
import pandas as pd
from django.utils.encoding import escape_uri_path
from scipy.interpolate import lagrange
from io import BytesIO
import base64


def miss_values(request):
    df = pd.read_csv('C:\PycharmProjects\dataAnalysis\data\hz_weather.csv')
    if request.method == "POST":
        select = request.POST.get("miss", None)
        if select == 'mean':
            data = df.fillna(df.mean())
            new_data = data.isnull().sum()
            print(new_data)
            data1 = data.head()
            csv_file = time_stamp = '{0:%Y%m%d%H%M}'.format(datetime.datetime.now())
            data2 = data.to_csv("media/csv/{}.csv".format(csv_file), index=0)  # 保存CSV数据
            print("平均值填补")
            return render(request, 'test.html', locals())
        if select == 'median':
            data = df.fillna(df.median())
            new_data = data.isnull().sum()
            print(new_data)
            data1 = data.head()
            print("中位数填补")
            return render(request, 'test.html', locals())
        if select == 'mode':
            data = df.fillna(df.mode())
            new_data = data.isnull().sum()
            print(new_data)
            data1 = data.head()
            print("众数填补")
            return render(request, 'test.html', locals())
        if select == 'delete_miss':
            data = df.dropna(axis=0)
            new_data = data.isnull().sum()
            print(new_data)
            data1 = data.head()
            print("删除缺失值")
            return render(request, 'test.html', locals())
        if select == 'inter':
            data = df.interpolate()
            new_data = data.isnull().sum()
            print(new_data)
            data1 = data.head()
            print("插值法")
            return render(request, 'test.html', locals())
    return render(request, 'test.html', locals())


# 文件下载
def download(request):
    local_filename = "media/csv/"
    filename = os.listdir(local_filename)
    filename.sort(key=lambda fn: os.path.getatime(local_filename + fn) if not os.path.isdir(local_filename + fn) else 0)
    # d = datetime.datetime.fromtimestamp(os.path.getmtime(local_filename + filename[-1]))
    # print('最新生成的文件是' + filename[-1] + "，时间：" + d.strftime("%Y{y}%m{m}%d{d}%H{h}%M{m1}%S{s}").format(y='年',m='月',d='日',h="时",m1="分",s="秒"))
    # print(filename[-1])
    file = open('media/csv/{}'.format(filename[-1]), 'rb')
    response = FileResponse(file)
    response['Content-Type'] = 'application/octet-stream'
    response['Content-Disposition'] = "attachment; filename*=utf-8''{}".format(escape_uri_path(filename[-1]))
    return response


def create_img(request):
    if request.method == "POST":
        df = pd.read_csv('data/hz_weather1.csv')
        select = request.POST.get("miss_values", None)
        # shapes = df.shape # 行列数
        # infos = df.info() # 数据类型
        describes = df.describe() # 数值型数据规模
        # miss_values = df.isnull().sum() # 数据缺失数量
        miss_data = dict(df.isnull().sum())  # 缺失情况
        # miss_rows = df.isnull().any(axis=1).sum() # 缺失的行数
        miss_is_or_not = dict(df.isnull().any(axis=0))  # 是否缺失
        miss_ratios = dict(df.isnull().sum(axis=0) / df.shape[0]) # 各列缺失比例
        means = dict(df.mean()) # 平均值
        maxs = dict(df.max()) # 最大值
        mins = dict(df.min())  # 最小值
        vars = dict(df.var()) # 方差
        stds = dict(df.std()) # 标准差
        # miss_ratio = str((df.isnull().any(axis=1).sum() / df.shape[0]) * 100)[:5] + '%' # 缺失观测的比例
        if select == 'mean':  # 箱型图
            print(select)
            src = box(df)
        elif select == 'median':  # 直方图
            print(select)
            src = imgs(df)
        elif select == 'mode':  # 小提琴
            print(select)
            src = violinplot(df)
        elif select == 'var':  # 变量
            variable = request.POST.get("vars", None)
            variable1 = variable.split(',')
            print(variable1)
            src = var_relation(df,variable1)
        elif select == 'var1':  # 变量
            variable = request.POST.get("vars", None)
            src = var_relation1(df, variable)
        elif select == 'relativity':  # 小提琴
            variable = request.POST.get("vars", None)
            variable1 = variable.split(',')
            print(variable1)
            src = relativity(df,variable1)
        # select = request.POST.get("miss", None)
        # matplotlib.use('Agg')  # 不出现画图的框
        # plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
        # plt.rcParams['axes.unicode_minus'] = False
        # # sns.pairplot(df[['最高气温','最低气温','日期']]) # 变量之间的关系
        # sns.violinplot(df['最高气温']) # 小提琴图等
        # sns.boxplot(df['最高气温']) # 箱线图
        # # sns.distplot(df['最高气温'], kde=False)  # 直方图
        # # 转成图片的步骤
        # sio = BytesIO()
        # plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
        # data = base64.encodebytes(sio.getvalue()).decode()
        # src = 'data:image/png;base64,' + str(data)
        # # # 记得关闭，不然画出来的图是重复的
        # plt.close()
        # # plt.show()
    return render(request, 'imgtest.html', locals())


# 直方图
def imgs(df):
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    sns.distplot(df['最高气温'])  # 直方图
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src


# 箱型图
def box(df):
    print(type(df))
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    # sns.boxplot(df['最高气温'])  # 箱线图
    msno.bar(df.sample(df.shape[0]))  # 柱状图
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src

# 小提琴图
def violinplot(df):
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    # sns.pairplot(df[['最高气温','最低气温','日期']]) # 变量之间的关系
    sns.violinplot(df['最高气温'])  # 小提琴图等
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src

# 变量关系
def var_relation(df,vars):
    var = vars
    print(var)
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    sns.pairplot(df[var],markers=["o", "s"])  # 变量之间的关系
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src

# 变量关系1
def var_relation1(df,vars):
    var = vars
    print(var)
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    sns.pairplot(df,hue=var)  # 变量之间的关系
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src

# 数据相关性探查
def relativity(df,vars):
    var = vars
    print(var)
    matplotlib.use('Agg')  # 不出现画图的框
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 这两行用来显示汉字
    plt.rcParams['axes.unicode_minus'] = False
    sns.heatmap(df[var].corr())  # 变量之间的关系
    sio = BytesIO()
    plt.savefig(sio, format='png', bbox_inches='tight', pad_inches=0.0)
    data = base64.encodebytes(sio.getvalue()).decode()
    src = 'data:image/png;base64,' + str(data)
    # # 记得关闭，不然画出来的图是重复的
    plt.close()
    return src